package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        
        departmentMapper.save(department);//保存之后，拿到自己生成的id

        if(department.getParent()==null){//没有上级
            department.setDirPath("/"+department.getId());
        }else{//给了上级部门id
            Long parent_id = department.getParent().getId();
            Department parent_dept = departmentMapper.loadById(parent_id);
            String dirPath = parent_dept.getDirPath();
            department.setDirPath(dirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if(department.getParent()==null){//没有上级
            department.setDirPath("/"+department.getId());
        }else{//给了上级部门id
            Long parent_id = department.getParent().getId();
            Department parent_dept = departmentMapper.loadById(parent_id);
            String dirPath = parent_dept.getDirPath();
            department.setDirPath(dirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
